package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.dto.UserCartPreviewDto;
import com.group7.mall.entity.TbGoodsInventoryPrice;
import com.group7.mall.dto.UserCartAddDto;
import com.group7.mall.entity.TbUser;
import com.group7.mall.entity.TbUserCart;
import com.group7.mall.service.CartService;
import com.group7.mall.service.UserGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("user-cart")
public class CustomerCartController {



    @Resource
    private CartService cartService;

    @Resource
    private UserGoodsService userGoodsService;

    @PostMapping("query-all-cart-number")
    public Result<Integer> queryAllCartNumber(@RequestBody TbUser tbUser) {
        return Result.ok(cartService.queryAllCartNumber(tbUser.getUserOpenId()));
    }

    @PostMapping("add-goods-to-cart")
    // TODO 将创建时间改成由前端传递（已完成）
    public Result<Integer> addGoodsToCart(@RequestBody UserCartAddDto userCartAddDto) {
        TbGoodsInventoryPrice tbGoodsInventoryPrice = userGoodsService.queryGoodsInventoryByTypeIds(userCartAddDto);
        Result result = Result.ok();
        if (tbGoodsInventoryPrice == null) {
            result.setMessage("错误！该商品已更改种类或商品已下架，请重新刷新页面");
            return result;
        }
        return result.ok(cartService.insert(userCartAddDto));
    }

    @PostMapping("query-goods-inventory-price")
    public Result<TbGoodsInventoryPrice> queryGoodsInventory(@RequestBody UserCartAddDto userCartAddDto) {
        TbGoodsInventoryPrice tbGoodsInventoryPrice = userGoodsService.queryGoodsInventoryByTypeIds(userCartAddDto);
        Result result = Result.ok();
        if (tbGoodsInventoryPrice == null) {
            result.setMessage("错误！该商品已更改种类或商品已下架，请重新刷新页面");
        } else if (userCartAddDto.getCount() > tbGoodsInventoryPrice.getInventory()) {
            result.setMessage("失败！购物车商品数量大于库存，请重新选择商品数量，当前种类商品实时库存为data的值");
            result.setData(tbGoodsInventoryPrice);
        } else {
            result.setMessage("成功！当前种类商品实时库存为data的值");
            result.setData(tbGoodsInventoryPrice);
        }
        return result;
    }

    @PostMapping("show-user-cart")
    public Result<List<UserCartPreviewDto>> showUserCart(@RequestBody TbUser tbUser) {
        return Result.ok(cartService.showUserCart(tbUser.getUserOpenId()));
    }

    @PostMapping("delete-user-cart")
    public Result<Integer> deleteUserCart(@RequestBody TbUserCart tbUserCart) {
        Result result = Result.ok();
        int i = cartService.deleteById(tbUserCart.getCartId());
        if (i == 1) {
            result.setMessage("删除购物车成功！");
            result.setData(1);
            return result;
        } else {
            result.setMessage("删除购物车失败！");
            result.setData(0);
            return result;
        }
    }

    @PostMapping("update-user-cart")
    public Result<Integer> updateUserCart(@RequestBody TbUserCart tbUserCart) {
        Result result = Result.ok();
        int i = cartService.updateCartNumberById(tbUserCart);
        if (i == 1) {
            result.setMessage("更新购物车成功！");
            result.setData(1);
            return result;
        } else {
            result.setMessage("更新购物车失败！");
            result.setData(0);
            return result;
        }
    }

    @PostMapping("query-goods-inventory2")
    Result<Integer> queryGoodsInventory2(@RequestBody UserCartAddDto userCartAddDto) {
        Result result = Result.ok();
        result.setData(userGoodsService.queryGoodsInventoryByCombinationId(userCartAddDto).getInventory());
        result.setMessage("成功！data为库存数量");
        return result;
    }


}
